from django.forms import ModelForm, DateTimeInput, DateInput, Select, TimeInput, Textarea, CheckboxInput, \
    inlineformset_factory, TextInput, NumberInput, RadioSelect

from .models import *


class TbModelForm(ModelForm):

    def __init__(self, *args, **kwargs):
        super(TbModelForm, self).__init__(*args, **kwargs)
        if self.fields:
            for k, v in self.fields.items():
                v.widget.attrs.update({'class': 'form-control'})
                if isinstance(v.widget, DateTimeInput):
                    v.widget.attrs.update(
                        {'class': 'form-control datetime-auto'})
                if isinstance(v.widget, DateInput):
                    v.widget.attrs.update(
                        {'class': 'form-control date-auto', 'data-date-format': 'yyyy-mm-dd'})
                if isinstance(v.widget, Select):
                    v.widget.attrs.update(
                        {'class': 'selectpicker show-tick form-control', 'data-live-search': 'true'})
                if isinstance(v.widget, TimeInput):
                    v.widget.attrs.update(
                        {'class': 'form-control time-auto'})
                if isinstance(v.widget, Textarea):
                    v.widget.attrs.update({'class': 'form-control summernote'})
                if isinstance(v.widget, CheckboxInput, ):
                    v.widget.attrs.update({'type': 'checkbox', 'class': ''})
                if isinstance(v.widget, RadioSelect, ):
                    v.widget.attrs.update({'type': 'radio', 'class': ''})

    def save(self, commit=True):
        return super(TbModelForm, self).save(commit)


class ApplicationCreateForm(TbModelForm):
    class Meta:
        model = Application
        exclude = ('approval_amount', 'approval_notes', 'pay_channel', 'date_end')


class ApplicationUpdateForm(TbModelForm):
    class Meta:
        model = Application
        exclude = ('code', 'approval_amount', 'approval_notes', 'pay_channel', 'created_by', 'date_end')
        widgets = {
            'default_approved_by': Select(attrs={'class': 'form-control', 'required': True}),
        }


class ApplicationSubmitForm(TbModelForm):
    class Meta:
        model = Application
        fields = ['status']


class ApplicationApproveForm(TbModelForm):
    class Meta:
        model = Application
        exclude = ['code', 'default_approved_by', 'created_by', 'pay_channel']
        widgets = {
            'name': TextInput(attrs={'class': 'form-control', 'readonly': True}),
            'initial_amount': NumberInput(attrs={'class': 'form-control', 'readonly': True}),
            'approval_amount': NumberInput(attrs={'class': 'form-control', 'required': True}),
            'initial_notes': Textarea(attrs={'class': 'form-control', 'readonly': True}),
        }


ApplicationAttachmentFormSet = inlineformset_factory(
    Application, ApplicationAttachment, fields=('file_name', 'file'), extra=3, can_delete=True, max_num=9,
)


class ProjectForm(TbModelForm):
    class Meta:
        model = Project
        fields = '__all__'
